package com.brspace.qualitycontrol.controller.admin;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brspace.qualitycontrol.entity.ProcessNode;
import com.brspace.qualitycontrol.entity.Publish;
import com.brspace.qualitycontrol.entity.PublishFile;
import com.brspace.qualitycontrol.entity.User;
import com.brspace.qualitycontrol.entity.vo.MonthNumVo;
import com.brspace.qualitycontrol.entity.vo.PaginationVo;
import com.brspace.qualitycontrol.entity.vo.PublishVo;
import com.brspace.qualitycontrol.entity.wps.entity.WFileT;
import com.brspace.qualitycontrol.mapper.ProcessNodeMapper;
import com.brspace.qualitycontrol.mapper.PublishMapper;
import com.brspace.qualitycontrol.mapper.UserMapper;
import com.brspace.qualitycontrol.service.*;

import com.brspace.qualitycontrol.service.impl.PublishServiceImpl;
import com.brspace.qualitycontrol.utils.EmptyUtil;
import com.brspace.qualitycontrol.utils.R;
import com.brspace.qualitycontrol.utils.WordUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 发文拟稿 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-04-02
 */
@Api(description = "公文拟稿")
@CrossOrigin
@RestController
@RequestMapping("/admin/publish")
public class PublishController {
    @Resource
    private PublishService publishService;
    @Resource
    private PublishUserService publishUserService;
    @Resource
    private PublishFileService publishFileService;
    @Resource
    private PublishMapper publishMapper;
    @Resource
    private ProcessNodeMapper processNodeMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private FileTService fileTService;
    /**
     * 新建发文拟稿
     * @param publish
     * @return
     */
    @ApiOperation("新建发文拟稿")
    @PostMapping("addPub")
    public R addPub(@RequestBody Publish publish){
        return publishService.addPub(publish);
    }


    /**
     *发文拟稿流程运转
     *@author Hugo
     *@date 2021-04-23 09:36
     **/
    @PostMapping("examine/{nextId}/{userId}")
    public R examine(@RequestBody Publish publish,@PathVariable String  nextId,@PathVariable String userId){
        publishService.examine(publish,nextId,userId);
        return  R.ok();
    }


    /**
     *显示代办事项
     *@author Hugo
     *@date 2021-04-26 14:59
     **/
    @PostMapping("pageUnCompleteList/{page}/{limit}/{userId}")
    public R pageUnCompleteList(@PathVariable int page,@PathVariable int limit,@PathVariable String userId){
        return R.ok().data("list",publishService.pageList(page,limit,userId)).data("total",publishService.pageList(page,limit,userId).size());
    }

    /**
     *显示已办事项
     *@author Hugo
     *@date 2021-04-26 15:54
     **/
    @PostMapping("pageCompleteList/{page}/{limit}/{userId}")
    public R pageCompleteList(@PathVariable int page,@PathVariable int limit,@PathVariable String userId){
        return R.ok().data("list",publishService.pageCompleteList(page,limit,userId)).data("total",publishService.pageCompleteList(page,limit,userId).size());
    }

    /**
     *显示全部事项
     *@author Hugo
     *@date 2021-04-26 16:13
     **/
    @PostMapping("getAll/{page}/{limit}/{userId}")
    public R getAll(@PathVariable int page,@PathVariable int limit,@PathVariable String userId){
        return R.ok().data("list",publishService.pageAllList(page,limit,userId)).data("total",publishService.pageAllList(page,limit,userId).size());

    }

    @ApiOperation(value = "保存发文拟稿", notes = "保存发文拟稿")
    @PostMapping("save")
    public R save(HttpServletRequest request,
                  HttpServletResponse response,
                  @RequestBody Publish publish){
        return publishService.savePublish(request,response,publish);
    }

    /**
     * 全部发文列表
     * @return
     */
    @ApiOperation("全部发文列表")
    @PostMapping("list/{page}/{limit}/{userId}")
    public R pageList(
            @ApiParam(value = "当前页码",required = true) @PathVariable Long page,
            @ApiParam(value = "每页多少数据", required = true) @PathVariable Long limit,
            @PathVariable String userId,
            @ApiParam("讲师列表查询条件") PublishVo publishVo){
        Page<Publish> pageParam = new Page<>(page, limit);
        IPage<Publish> pageModel = publishService.selectPage(pageParam, publishVo,userId);
        List<Publish> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "分页查询当前用户未阅发文列表", notes = "分页查询当前用户未阅发文列表")
    @PostMapping("getNoPostList")
    public R getNoPostList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize") Integer pageSize){
        //当前登录的用户Id
        String userId = "1376058930834300930";
        PaginationVo<Publish> page = publishService.getPostList(pageNum,pageSize,userId,0);
        return R.ok().data("data",page);
    }

    @ApiOperation(value = "分页查询当前用户已阅发文列表", notes = "分页查询当前用户已阅发文列表")
    @PostMapping("getYesPostList")
    public R getYesPostList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize") Integer pageSize){
        //当前登录的用户Id
        String userId = "1376058930834300930";
        PaginationVo<Publish> page = publishService.getPostList(pageNum,pageSize,userId,1);
        return R.ok().data("data",page);
    }

    @ApiOperation(value = "根据id查询发文详情", notes = "根据id查询发文详情")
    @PostMapping("selectPostDraftById/{userId}")
    public R findInStockList(Publish publish,@PathVariable String userId){
        Publish byId = publishService.getById(publish.getId());
        List<PublishFile> publishFile = publishFileService.list(new QueryWrapper<PublishFile>().eq("p_id",publish.getId()).eq("file_type",0));
        PublishFile one = publishFileService.getOne(new QueryWrapper<PublishFile>().eq("p_id", publish.getId()).eq("file_type", 1));
        byId.setAnnex(publishFile);
        WFileT wFileT = fileTService.getOne(new QueryWrapper<WFileT>().eq("id", one.getWId()));
        int s = 0;
        if (publishMapper.selectById(publish).getNodeId() != 0 ){
           s = processNodeMapper.selectById(publishMapper.selectById(publish).getNodeId()).getSortNum();
        }

        return R.ok().data("data",byId).data("number",s).data("fileId",wFileT.getId()).data("role",userMapper.selectOne(new QueryWrapper<User>().eq("dept_id",userMapper.selectById(userId).getDeptId()).eq("role","文书")).getId());
    }

    @ApiOperation(value = "每月统计发文数量", notes = "每月统计发文数量")
    @PostMapping("statisticsByMonth")
    public R statisticsByMonth(){
        List<MonthNumVo> list = publishService.statisticsByMonth();
        return R.ok().data("data",list);
    }

    @ApiOperation(value = "计算今年发文数量和去年发文数量", notes = "计算今年发文数量和去年发文数量")
    @PostMapping("nowAndLastNum")
    public R nowAndLastNum(){
        return publishService.getNowAndLastNum();
    }

    @ApiOperation(value = "发文类型统计列表", notes = "发文类型统计列表")
    @PostMapping("typeNumList")
    public R typeNumList(){
        return publishService.getTypeNumList();
    }


}

